package net.helix.ui.panels.library;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import net.helix.engine.EngineTop;
import net.helix.engine.library.LibraryTreeNode;

public class LibraryPathArrayTransferable implements Transferable{
	private LibraryTreeNode[] data = null;
	private LibraryTreeNode dataIfLibraryItem = null;
	private static DataFlavor[] flavors = new DataFlavor[3];
	public static DataFlavor LibraryTreeNodeFlavor = new DataFlavor(LibraryTreeNode[].class, "LibraryTreeNode Array");
	public static DataFlavor LoadLibraryPathArrayTransferable = new DataFlavor(boolean.class, "LibraryPathArrayTransferable");
	static{
		flavors[0] = LibraryTreeNodeFlavor;
		flavors[1] = DataFlavor.stringFlavor;
		flavors[2] = LoadLibraryPathArrayTransferable;
		// TODO: add a flavor type for image, so you can drag images from the library to outside of the program...
	}
	public LibraryPathArrayTransferable(LibraryTreeNode[] data){
		super();
		this.data = data;
	}
	public LibraryPathArrayTransferable(LibraryTreeNode data){
		super();
		dataIfLibraryItem = data;
	}
	@Override
	public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
		
		if(flavor == DataFlavor.stringFlavor){
			String str = "";
			if(data != null){
				for(int i=0;i<data.length;i++){
					
					LibraryTreeNode node = data[i];
					//if(node instanceof LibraryTreeNode){
						//LibraryTreeNode _node = (LibraryTreeNode) node;
						
						if(i>0){
							str += ", "+node.getName();
						}else{
							str = node.getName();
						}
					//}
				}
			}else if(dataIfLibraryItem != null){
				str = dataIfLibraryItem.getName();
			}

			return str;
		}else if(flavor == LibraryTreeNodeFlavor){
			if(dataIfLibraryItem != null){
				return new LibraryTreeNode[]{dataIfLibraryItem};
			}else{
				/*
				String[] strArray  = new String[data.length];
				for(int i=0;i<data.length;i++){
					String str = "";
					Object[] obj = data[i].getPath();
					str = ((LibraryTreeNode)(obj[i])).getName();
					
					strArray[i] = str;
				}
				*/
				
				return data;
			}
			
		}else if(flavor == LoadLibraryPathArrayTransferable){
			
			if(dataIfLibraryItem != null){
				EngineTop.setLibraryTransferableData(new LibraryTreeNode[]{dataIfLibraryItem});
			}else{
				EngineTop.setLibraryTransferableData(data);
			}
			return Boolean.TRUE;
		}else{
			return null;
		}
	}

	@Override
	public DataFlavor[] getTransferDataFlavors() {
		return flavors;
	}

	@Override
	public boolean isDataFlavorSupported(DataFlavor flavor) {
		if(flavor == flavors[0]){
			return true;
		}else if(flavor == flavors[1]){
			return true;
		}
		return false;
	}

}
